/*例9-6 select调用与非阻塞Socket*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 8888
#define BACKLOG 5
#define BUF_SIZE 200

int fd_A[BACKLOG];
int conn_amount;

void show_client() {
    printf("client amount:%d\n", conn_amount);
}

int main(void) {
    int sock_fd, new_fd;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    socklen_t sin_size;
    int yes = 1;
    char buf[BUF_SIZE];
    int ret;
    int i;

    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(MYPORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));

    if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sock_fd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    printf("listen port %d\n", MYPORT);

    fd_set fdsr;
    int maxsock = sock_fd;

    conn_amount = 0;
    sin_size = sizeof(client_addr);

    while (1) {
        FD_ZERO(&fdsr);
        FD_SET(sock_fd, &fdsr);

        for (i = 0; i < conn_amount; i++) {
            FD_SET(fd_A[i], &fdsr);
        }

        struct timeval tv;
        tv.tv_sec = 30;
        tv.tv_usec = 0;

        int ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv);

        if (ret == -1) {
            perror("select");
            break;
        } else if (ret == 0) {
            printf("timeout\n");
            continue;
        }

        for (i = 0; i < conn_amount; i++) {
            if (FD_ISSET(fd_A[i], &fdsr)) {
                ret = recv(fd_A[i], buf, sizeof(buf), 0);
                if (ret <= 0) {
                    printf("client %d close\n", i);
                    close(fd_A[i]);
                    FD_CLR(fd_A[i], &fdsr);
                    conn_amount--;
                    for (int j = i; j < conn_amount; j++) {
                        fd_A[j] = fd_A[j + 1];
                    }
                    show_client();
                    break;
                } else {
                    buf[ret] = '\0';
                    printf("client %d send:%s\n", i, buf);
                }
            }
        }

        if (FD_ISSET(sock_fd, &fdsr)) {
            new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size);
            if (new_fd == -1) {
                perror("accept");
                continue;
            }

            if (conn_amount < BACKLOG) {
                fd_A[conn_amount++] = new_fd;
                printf("new connection client[%d] IP:%s port:%d\n", conn_amount - 1,
                       inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
                if (new_fd > maxsock) {
                    maxsock = new_fd;
                }
            } else {
                printf("max connections arrive,exit\n");
                send(new_fd, "bye", 4, 0);
                close(new_fd);
                break;
            }
            show_client();
        }
    }

    for (i = 0; i < BACKLOG; i++) {
        if (fd_A[i] != 0) {
            close(fd_A[i]);
        }
    }
    close(sock_fd);
    exit(0);
}